package com.example.myandroidapp1.sql.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.myandroidapp1.sql.DBHelper;

public class SqlDbService {

	public static final String KEY_ROWID = "_id";
	public static final String KEY_NAME = "persons_name";
	public static final String KEY_HOTNESS = "persons_hotness";

	private static final String DATABASE_NAME = "HotOrNotDb";
	private static final String DATABASE_TABLE = "People";
	private static final int DATABASE_VERSION = 1;

	private DBHelper dbHelper;
	private Context context;
	private SQLiteDatabase ourDatabase;

	public SqlDbService(Context c) {
		super();
		context = c;

	}

	public void open() {
		dbHelper = new DBHelper(context);
		ourDatabase = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
		// ourDatabase.close();
	}

	public long createEntry(String name, String hotness) {
		// TODO Auto-generated method stub
		ContentValues cv = new ContentValues();
		cv.put(KEY_NAME, name);
		cv.put(KEY_HOTNESS, hotness);
		return ourDatabase.insert(DATABASE_TABLE, null, cv);

	}

	public String getData() {
		// TODO Auto-generated method stub
		String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_HOTNESS };
		Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
		String result = "";
		
		int iRow = c.getColumnIndex(KEY_ROWID);
		int iName = c.getColumnIndex(KEY_NAME);
		int iHotness = c.getColumnIndex(KEY_HOTNESS);
		
		for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
			result = result + c.getString(iRow) +  " " + c.getString(iName) + " " + c.getString(iHotness) + "\n";
		}
		return result;
	}

	public String getName(long l) {
		// TODO Auto-generated method stub
		String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_HOTNESS };
		Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID +  "=" + l, null, null, null, null);
		if(c != null){
			c.moveToFirst();
			String name = c.getString(1);
			return name;
		}
		return null;
	}

	public String getHotness(long l) {
		// TODO Auto-generated method stub
		String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_HOTNESS };
		Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID +  "=" + l, null, null, null, null);
		if(c != null){
			c.moveToFirst();
			String hotness = c.getString(2);
			return hotness;
		}
		return null;
	}

	public void updateEntry(long row, String name, String hotness) {
		// TODO Auto-generated method stub
		ContentValues cv = new ContentValues();
		cv.put(KEY_NAME, name);
		cv.put(KEY_HOTNESS, hotness);
		ourDatabase.update(DATABASE_TABLE, cv, KEY_ROWID +  "=" + row,null);
		
		
	}

	public void deleteEntry(long rowId) {
		// TODO Auto-generated method stub
		ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + " = " + rowId, null);
	}
}
